﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Hkwl.WorldExpo.Merchant.Common;
using Hkwl.WorldExpo.Merchant.Model;
using Hkwl.WorldExpo.Merchant.Service;

namespace Hkwl.WorldExpo.Merchant.Web
{
    public partial class ModifyProject : BaseCheckPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int projectId = int.Parse(Request.QueryString["ProjectId"]);
                NxProject nxProject = ServiceLocator.IProjectService.GetNxProjectById(projectId);
                this.txtProjectName.Text = nxProject.ProjectName;
                this.txtProjectDescription.Text = nxProject.ProjectDescribe;
                this.txtStartTime.Text = nxProject.StartTime.ToString("yyyy-MM-dd");
                this.txtEndTime.Text = nxProject.EndTime.ToString("yyyy-MM-dd");
                this.txtMMSTitle.Text = nxProject.MmsTitle;
                this.hidOldProject.Value = nxProject.ProjectName;
                int merchantId = 1;
                if (Session["MerchantId"] != null)
                {
                    merchantId = int.Parse(Session["MerchantId"].ToString());
                } 
                this.ddlGroup.DataSource = ServiceLocator.IClientService.GetNxGroupCollection(merchantId);              
                this.ddlGroup.DataTextField = "GroupName";
                this.ddlGroup.DataValueField = "GroupId";
                this.ddlGroup.DataBind();
                if (nxProject.GroupId == 0)
                {
                    this.ddlGroup.Items.Add(new ListItem((String)GetGlobalResourceObject("Default", "ALL_CUSTOMERS"), "0"));
                    this.ddlGroup.Items[ddlGroup.Items.Count - 1].Selected = true;
                }
                else
                {
                    this.ddlGroup.Items.Add(new ListItem((String)GetGlobalResourceObject("Default", "ALL_CUSTOMERS"), "0"));
                    for (int i = 0; i < this.ddlGroup.Items.Count; i++)
                    {
                        if (this.ddlGroup.Items[i].Value == nxProject.GroupId.ToString())
                        {
                            this.ddlGroup.Items[i].Selected = true;
                        }
                    }
                }
                
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int projectId = int.Parse(Request.QueryString["ProjectId"]);
            NxProject nxProject = ServiceLocator.IProjectService.GetNxProjectById(projectId);
            nxProject.ProjectName = this.txtProjectName.Text.Trim();
            nxProject.ProjectDescribe = this.txtProjectDescription.Text.Trim();
            nxProject.StartTime = Convert.ToDateTime(this.txtStartTime.Text.Trim());
            nxProject.EndTime = Convert.ToDateTime(this.txtEndTime.Text.Trim());
            nxProject.GroupId = int.Parse(ddlGroup.SelectedValue);
            nxProject.MmsTitle = this.txtMMSTitle.Text;
            if (ServiceLocator.IProjectService.UpdateNxProject(nxProject))
            {
                JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "SAVE_SUCCESSFUL") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "','ProjectList.aspx')", this.Page);
            }
        }

        protected void btnModifyMms_Click(object sender, EventArgs e)
        {
            int projectId = int.Parse(Request.QueryString["ProjectId"]);
            
            NxProject nxProject = ServiceLocator.IProjectService.GetNxProjectById(projectId);
            Response.Redirect("ModifyMMSList.aspx?ProjectId=" + nxProject.ProjectId);
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            JavaScript.WriteJs("history.go(-2);", this.Page);
        }
    }
}
